package com.zzyl.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.mapper.NursingProjectPlanMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.vo.NursingProject;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description NursingProjectService
 * @Author Lwj
 * @Date 2024-08-23-10:35
 */
@Service
public class NursingProjectServiceImpl implements NursingProjectService {
    @Autowired
    private NursingProjectMapper nursingProjectMapper;

    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    /**
     * @param name     护理项目名称
     * @param status   状态：0-禁用，1-启用
     * @param pageNum  当前页码
     * @param pageSize 每页显示数量
     * @return 护理项目列表
     */
    @Override
    public PageResponse<NursingProjectVo> getByPage(String name, Integer status, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<NursingProjectVo> page = nursingProjectMapper.selectByPage(name, status);
        page.forEach(n->{
            Long id = n.getId();
            Long count=  nursingProjectPlanMapper.getCount(id);
            n.setCount(Math.toIntExact(count));
        });
        PageResponse<NursingProjectVo> pageResponse = new PageResponse(page);
        pageResponse.setRecords(page.getResult());
        return pageResponse;
    }

    /**
     * 新增护理项目
     *
     * @param nursingProjectDto 护理项目数据传输对象
     */
    @Override
    public void addNursingProject(NursingProjectDto nursingProjectDto) {
        NursingProject nursingProject = new NursingProject();
        BeanUtils.copyProperties(nursingProjectDto, nursingProject);
        nursingProjectMapper.insert(nursingProject);
    }

    /**
     * 根据id查询护理
     *
     * @param id
     * @return
     */
    @Override
    public NursingProjectVo getNursingProjectById(String id) {

        NursingProject nursingProject = nursingProjectMapper.search(id);
        return BeanUtil.toBean(nursingProject, NursingProjectVo.class);
    }

    /**
     * 编辑护理项目
     *
     * @param nursingProjectDto
     */
    @Override
    public void updateNursingProject(NursingProjectDto nursingProjectDto) {
        NursingProject nursingProject = new NursingProject();
        BeanUtils.copyProperties(nursingProjectDto, nursingProject);
        nursingProjectMapper.update(nursingProject);
    }

    /**
     * 删除护理项目
     *
     * @param id
     */
    @Override
    public void deleteNursingProject(Long id) {
        nursingProjectMapper.delete(id);
    }

    /**
     * 项目启用禁用
     * @param id
     * @param status 状态（0：禁用，1：启用
     */
    @Override
    public void ResetStatus(Long id, int status) {
        nursingProjectMapper.updateStatus(id, status);
    }

    /**
     * 查询所有护理项目
     * @return
     */
    @Override
    public List<NursingProjectVo> getAllNursingProject() {
       List<NursingProjectVo> NursingProjectVo= nursingProjectMapper.searchAll();
        return NursingProjectVo;
    }

}
